package com.hzjy.api.task;

import com.hzjy.api.beans.HzjyHotOriginators;
import com.hzjy.api.service.impl.ApiProductServiceImpl;
import com.hzjy.api.service.impl.HzjyHotOriginatorsServiceImpl;
import com.hzjy.common.utils.JSONUtil;
import com.hzjy.common.utils.PageData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;

/**
 * 热门权益人榜单任务
 */
@Component
public class HotTradingListTask {
    private static final Logger log = LoggerFactory.getLogger(HotTradingListTask.class);

    @Autowired
    private HzjyHotOriginatorsServiceImpl hzjyHotOriginatorsServiceImpl;
    @Autowired
    private ApiProductServiceImpl apiProductServiceImpl;

    /**
     * 每天0点更新热门权益人榜单
     */
    @Transactional
    public void createHotTradingList() {
        log.info("===========================【开始更新热门权益人榜单】====================================");
        Date now = new Date();
        List<PageData> pdList = apiProductServiceImpl.queryTradingProductByToday(now);
        log.info("查询到热门权益人榜单数据:" + JSONUtil.list2json(pdList));
        if (!CollectionUtils.isEmpty(pdList)) {
            hzjyHotOriginatorsServiceImpl.deleteHzjyHotOriginators();
            for (PageData pd : pdList) {
                HzjyHotOriginators hzjyHotOriginators = new HzjyHotOriginators();
                hzjyHotOriginators.setUpdateTime(now);
                hzjyHotOriginators.setUserId(pd.get("user_id").toString());
                hzjyHotOriginators.setWorkNum(Long.valueOf(pd.get("total").toString()));
                hzjyHotOriginatorsServiceImpl.insertHzjyHotOriginators(hzjyHotOriginators);
            }
        }
        log.info("===========================【更新热门权益人榜单结束】====================================");
    }
}
